package com.vitabuybuy.tv.ui.home.bidd.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.*
import com.vitabuybuy.tv.inter.OnIdItemClickListener
import com.vitabuybuy.tv.network.AddressRepository
import com.vitabuybuy.tv.network.BiddingRepository
import com.vitabuybuy.tv.network.ShopCenterRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class BiddingZoneViewModel : BaseViewModel() {

    val bidderlist = ObservableArrayList<BiddingRecordDto>()
    val bidderBinding = ItemBinding.of<BiddingRecordDto>(BR.itemBean, R.layout.item_bidding_record)
    val bidderBinding1 =
        ItemBinding.of<BiddingRecordDto>(BR.itemBean, R.layout.item_bidding_record_1)
    val bidderlist2 = ObservableArrayList<BiddingRecordDto>()
    val bidderBinding2 = ItemBinding.of<BiddingRecordDto>(BR.itemBean, R.layout.item_bidding)
    val goodslist = MutableLiveData<MutableList<BiddingListDto>>()
    fun getGoodsList(categoryId: Int, status: String, hotkey: String?, pageNumber: Int) {
        val map = HashMap<String, Any?>()
        if (categoryId != -1) map["category"] = mapOf(Pair("id", categoryId))
        if (status.isNotEmpty()) map["status"] = status
        if (!hotkey.isNullOrEmpty()) map["name"] = hotkey
        rxHttpRequest {
            onRequest = {
                goodslist.value = BiddingRepository.getBiddingList(map, pageNumber).await()
                defUI.msgEvent.postValue(MessageModel(1, arg1 = goodslist.value?.size ?: 0))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }

    val goodDetailDto = MutableLiveData<BiddingDetailDto>()

    fun  getGoodsDetail(goodsId: Int?) {
        rxHttpRequest {
            onRequest = {
                goodDetailDto.value = BiddingRepository.getBinddingDetail(goodsId).await()
//                defUI.msgEvent.postValue(MessageModel(1, arg1 = goodslist.value?.size ?: 0))
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }

    fun getBidderList(biddingId: Int?, pageNumber: Int, pageSize: Int) {
        rxHttpRequest {
            onRequest = {
                val it = BiddingRepository.getBiddingRecord(biddingId, pageNumber, pageSize).await()
                if (pageNumber == 1) {
                    bidderlist.clear()
                }
                bidderlist.addAll(it)
                if (pageSize != 3) {//不为3的时候，就证明是详情里的请求
                    if (bidderlist.isEmpty()) {
                        statusView.set(LoadingType.SHOW_EMPTY)
                    }else{
                        statusView.set(LoadingType.SHOW_CONTENT)
                    }
                    defUI.msgEvent.postValue(MessageModel(1, arg1 = bidderlist.size))
                }
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = pageSize == 10
            isRefresh = pageNumber == 1
        }
    }

    val addressList = MutableLiveData<MutableList<AddressListDto>>()
    fun getAddressList() {
        rxHttpRequest {
            onRequest = {
                addressList.value = AddressRepository.getAddressList().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    val auctionHallDto = MutableLiveData<AuctionHallDto>()
    val isFirst = ObservableInt(-1)
    fun toAuctionHall(biddingId: Int, times: Int) {
        isFirst.set(times)
        rxHttpRequest {
            onRequest = {
                auctionHallDto.value = BiddingRepository.toAuctionHall(biddingId).await()
                bidderlist2.clear()
                auctionHallDto.value?.bidRecordList?.run {
                    bidderlist2.addAll(this)
                }
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun toAuction(biddingId: Int, price: Double) {
        rxHttpRequest {
            onRequest = {
                BiddingRepository.toAuctionBidding(biddingId, price).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun collectOrcancel(id: Int, isFav: Boolean) {
        rxHttpRequest {
            onRequest = {
                BiddingRepository.collectOrcancel(id, isFav) .await()
                defUI.msgEvent.postValue(MessageModel(2, obj = isFav))
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun remindOrCancelBidding(id: Int, isRemid: Boolean) {
        rxHttpRequest {
            onRequest = {
                BiddingRepository.remindOrCancelBidding(id, isRemid) .await()
                defUI.msgEvent.postValue(MessageModel(3, obj = isRemid))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    val recommendlist =
        MutableLiveData<MutableList<GoodsRecommendAndSimilerDto.RecommendProductListBean>>()
    val similerlist =
        MutableLiveData<MutableList<GoodsRecommendAndSimilerDto.SimilarProductListBean>>()

    val itemOnClickListener = object : OnIdItemClickListener {
        override fun onItemClick(goodId: Int) {
            defUI.msgEvent.postValue(MessageModel(4, arg1 = goodId))
        }
    }
    val recommendBinding: OnItemBind<GoodsRecommendAndSimilerDto.RecommendProductListBean> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: GoodsRecommendAndSimilerDto.RecommendProductListBean ->
            itemBinding.set(BR.itemBean, R.layout.item_commodity_3).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    val similerBinding: OnItemBind<GoodsRecommendAndSimilerDto.SimilarProductListBean> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: GoodsRecommendAndSimilerDto.SimilarProductListBean ->
            itemBinding.set(BR.itemBean, R.layout.item_commodity_4).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    fun getGoodsList(goodId: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = ShopCenterRepository.getGoodsRecommendAndSimilerList(goodId)  .await()
                recommendlist.value = it.recommendProductList
                similerlist.value = it.similarProductList
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }
    override fun onRetry() {
        defUI.msgEvent.postValue(MessageModel(0))
    }
}
